package com.shrct.businessprocess.product.impl;

import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.organization.RCOrganizationService;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.product.RCIdentifiableNormalProductEntry;
import com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryDAO;
import com.shrct.businessprocess.product.RCNormalizerService;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.RCRegisterIdentifiableNormalPruductEvent;
import com.shrct.businessprocess.product.RCUnregisterIdentifiableNormalPruductEvent;
import com.shrct.businessprocess.product.impl.RCIdentifiableNormalProductEntryImpl;
import com.shrct.businessprocess.product.impl.RCRegisterIdentifiableNormalPruductEventImpl;
import com.shrct.businessprocess.product.impl.RCUnregisterIdentifiableNormalPruductEventImpl;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.purchase.RCIdentifiableProductInStockEvent;
import com.shrct.businessprocess.purchase.RCPurchaseOrder;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import com.shrct.businessprocess.purchase.impl.RCIdentifiableProductInStockEventImpl;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.security.User;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryDAO")
public class RCIdentifiableNormalProductEntryDAOImpl extends BaseDAOService<RCIdentifiableNormalProductEntry> implements RCIdentifiableNormalProductEntryDAO {
  @Autowired
  private ISystemUserService systemUserService;
  
  @Autowired
  private RCOrganizationService organizationService;
  
  @Autowired
  private RCNormalizerService normalizerService;
  
  /**
   * 普通产品登记。主要用于初始化数据的导入
   */
  public RCIdentifiableNormalProductEntry registerNormalProduct(final String projectId, final RCEnterprise enterprise, final String sid) {RCIdentifiableNormalProductEntry rCIdentifiableNormalProductEntry=RCIdentifiableNormalProductEntryImpl.create(getObjectFactory());
    final Procedure1<RCIdentifiableNormalProductEntry> _function = new Procedure1<RCIdentifiableNormalProductEntry>() {
        public void apply(final RCIdentifiableNormalProductEntry normalProduct) {
          normalProduct.setProjectId(projectId);
          String _upperCase = sid.toUpperCase();
          normalProduct.setSid(_upperCase);
          String _normalizeSid = RCIdentifiableNormalProductEntryDAOImpl.this.normalizerService.normalizeSid(sid);
          normalProduct.setNormalizedSid(_normalizeSid);
          normalProduct.setEnterprise(enterprise);
          normalProduct.start();RCRegisterIdentifiableNormalPruductEvent rCRegisterIdentifiableNormalPruductEvent=RCRegisterIdentifiableNormalPruductEventImpl.create(normalProduct,enterprise,getObjectFactory());
          final Procedure1<RCRegisterIdentifiableNormalPruductEvent> _function = new Procedure1<RCRegisterIdentifiableNormalPruductEvent>() {
              public void apply(final RCRegisterIdentifiableNormalPruductEvent it) {
                Date _day = DateUtil.today();
                it.setDate(_day);
                User _currentUser = RCIdentifiableNormalProductEntryDAOImpl.this.systemUserService.getCurrentUser();
                it.setOperator(_currentUser);
              }
            };
          ObjectExtensions.<RCRegisterIdentifiableNormalPruductEvent>operator_doubleArrow(rCRegisterIdentifiableNormalPruductEvent, _function);
        }
      };
    RCIdentifiableNormalProductEntry _doubleArrow = ObjectExtensions.<RCIdentifiableNormalProductEntry>operator_doubleArrow(rCIdentifiableNormalProductEntry, _function);
    return _doubleArrow;
  }
  
  public RCIdentifiableNormalProductEntry inStockNormalProduct(final String sid, final RCPurchaseOrderItem orderItem) {
    RCIdentifiableNormalProductEntry _xblockexpression = null;
    {
      RCPurchaseOrder _purchaseOrder = orderItem.getPurchaseOrder();
      RCProject _project = _purchaseOrder.getProject();
      String _code = _project.getCode();
      RCEnterprise _hostEnterprise = this.organizationService.getHostEnterprise();
      RCIdentifiableNormalProductEntry _registerNormalProduct = this.registerNormalProduct(_code, _hostEnterprise, sid);
      final Procedure1<RCIdentifiableNormalProductEntry> _function = new Procedure1<RCIdentifiableNormalProductEntry>() {
          public void apply(final RCIdentifiableNormalProductEntry p) {
            RCProductSpecification _productSpec = orderItem.getProductSpec();
            p.setType(_productSpec);
            RCPurchaseOrder _purchaseOrder = orderItem.getPurchaseOrder();
            RCSupplier _supplier = _purchaseOrder.getSupplier();
            p.setSupplier(_supplier);
            p._inStock(orderItem);RCIdentifiableProductInStockEvent rCIdentifiableProductInStockEvent=RCIdentifiableProductInStockEventImpl.create(getObjectFactory());
            final Procedure1<RCIdentifiableProductInStockEvent> _function = new Procedure1<RCIdentifiableProductInStockEvent>() {
                public void apply(final RCIdentifiableProductInStockEvent it) {
                  it.setProduct(p);
                  it.setQuantity(Integer.valueOf(1));
                  Date _day = DateUtil.today();
                  it.setDate(_day);
                  it.setOrderItem(orderItem);
                }
              };
            ObjectExtensions.<RCIdentifiableProductInStockEvent>operator_doubleArrow(rCIdentifiableProductInStockEvent, _function);
          }
        };
      final RCIdentifiableNormalProductEntry result = ObjectExtensions.<RCIdentifiableNormalProductEntry>operator_doubleArrow(_registerNormalProduct, _function);
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public void cancelInStockNormalProduct(final RCIdentifiableNormalProductEntry product) {
    IGenericQuery<RCIdentifiableProductInStockEvent> query=getObjectFactory().createGenericQuery(RCIdentifiableProductInStockEvent.class,"select e3 from com.shrct.businessprocess.purchase.impl.RCIdentifiableProductInStockEventImpl as e3   where ((e3.product) = (:product)) and  e3.active = 1      ").setParameter("product",product);
    final RCIdentifiableProductInStockEvent stockEvent = query.uniqueResult();getObjectFactory().delete(stockEvent);
    getObjectFactory().delete(product);
    
  }
  
  public void transferNormalPruduct(final RCIdentifiableNormalProductEntry product, final RCEnterprise enterprise) {
    RCEnterprise _enterprise = product.getEnterprise();RCUnregisterIdentifiableNormalPruductEvent rCUnregisterIdentifiableNormalPruductEvent=RCUnregisterIdentifiableNormalPruductEventImpl.create(product,_enterprise,getObjectFactory());
    final Procedure1<RCUnregisterIdentifiableNormalPruductEvent> _function = new Procedure1<RCUnregisterIdentifiableNormalPruductEvent>() {
        public void apply(final RCUnregisterIdentifiableNormalPruductEvent it) {
          Date _day = DateUtil.today();
          it.setDate(_day);
          User _currentUser = RCIdentifiableNormalProductEntryDAOImpl.this.systemUserService.getCurrentUser();
          it.setOperator(_currentUser);
        }
      };
    ObjectExtensions.<RCUnregisterIdentifiableNormalPruductEvent>operator_doubleArrow(rCUnregisterIdentifiableNormalPruductEvent, _function);
    product.setEnterprise(enterprise);RCRegisterIdentifiableNormalPruductEvent rCRegisterIdentifiableNormalPruductEvent=RCRegisterIdentifiableNormalPruductEventImpl.create(product,enterprise,getObjectFactory());
    final Procedure1<RCRegisterIdentifiableNormalPruductEvent> _function_1 = new Procedure1<RCRegisterIdentifiableNormalPruductEvent>() {
        public void apply(final RCRegisterIdentifiableNormalPruductEvent it) {
          Date _day = DateUtil.today();
          it.setDate(_day);
          User _currentUser = RCIdentifiableNormalProductEntryDAOImpl.this.systemUserService.getCurrentUser();
          it.setOperator(_currentUser);
        }
      };
    ObjectExtensions.<RCRegisterIdentifiableNormalPruductEvent>operator_doubleArrow(rCRegisterIdentifiableNormalPruductEvent, _function_1);
    product._deliver(enterprise);
  }
  
  public void cancelTransferNormalPruduct(final RCIdentifiableNormalProductEntry product) {
    RCEnterprise _enterprise = product.getEnterprise();RCUnregisterIdentifiableNormalPruductEvent rCUnregisterIdentifiableNormalPruductEvent=RCUnregisterIdentifiableNormalPruductEventImpl.create(product,_enterprise,getObjectFactory());
    final Procedure1<RCUnregisterIdentifiableNormalPruductEvent> _function = new Procedure1<RCUnregisterIdentifiableNormalPruductEvent>() {
        public void apply(final RCUnregisterIdentifiableNormalPruductEvent it) {
          Date _day = DateUtil.today();
          it.setDate(_day);
          User _currentUser = RCIdentifiableNormalProductEntryDAOImpl.this.systemUserService.getCurrentUser();
          it.setOperator(_currentUser);
        }
      };
    ObjectExtensions.<RCUnregisterIdentifiableNormalPruductEvent>operator_doubleArrow(rCUnregisterIdentifiableNormalPruductEvent, _function);
    final RCEnterprise hostEnterprise = this.organizationService.getHostEnterprise();
    RCEnterprise _hostEnterprise = this.organizationService.getHostEnterprise();
    product.setEnterprise(_hostEnterprise);RCRegisterIdentifiableNormalPruductEvent rCRegisterIdentifiableNormalPruductEvent=RCRegisterIdentifiableNormalPruductEventImpl.create(product,hostEnterprise,getObjectFactory());
    final Procedure1<RCRegisterIdentifiableNormalPruductEvent> _function_1 = new Procedure1<RCRegisterIdentifiableNormalPruductEvent>() {
        public void apply(final RCRegisterIdentifiableNormalPruductEvent it) {
          Date _day = DateUtil.today();
          it.setDate(_day);
          User _currentUser = RCIdentifiableNormalProductEntryDAOImpl.this.systemUserService.getCurrentUser();
          it.setOperator(_currentUser);
        }
      };
    ObjectExtensions.<RCRegisterIdentifiableNormalPruductEvent>operator_doubleArrow(rCRegisterIdentifiableNormalPruductEvent, _function_1);
    product._return();
  }
}
